const fileService = require('../service/file.service')
const userinfoService = require('../service/userinfo.service')
const {APP_HOST, APP_PORT} = require('../app/config')

class FileController {
  async saveAvatar(ctx, next) {
    const {size, mimetype, filename} = ctx.req.file;
    const userId = ctx.user.id;
    try {
      const result = await fileService.createAvatar(userId, filename, mimetype, size)
      // 更新请求头像地址
      const avatarUrl = `${APP_HOST}:${APP_PORT}/userinfo/${userId}/avatar`
      await userinfoService.updateAvatarUrlById(avatarUrl, userId)
      ctx.success({
        msg: '上传头像成功'
      })
    } catch (error) {
      console.log(error.message)
      return
    }
  }
}

module.exports = new FileController() 